<?php
$loader->requireOnce('includes/Datasource_sql.class.php');
class FeeCoefficient extends ORDataObject {
	var $fee_coefficient_id	=	'';
	var $service_id			=	'';
  	var $person_id			=	'';
  	var $role				=	'';
  	var $type				=	'';
  	var $fixed_value		=	'';
  	var $effective_date		=	'';
	
	var $_table					=	'fee_coefficient';
	var $_key					=	'fee_coefficient_id';
	var	$_internalName			=	'FeeCoefficient';
	var $_foreignKeyList		=	array(
		'person_id'		=>	'employee',
		'service_id'	=>	'service'
	);
	
	var $date_format	=	'';
	
	public function FeeCoefficient(){
		parent::ORDataObject();
		$this->date_format	=	L10nHelper::getSqlDateFormat();
	}
	
	public function setup($id = 0){
		$this->set('id',$id);
		if($id>0){
			$this->populate();
		}
	}
	
	public function populate(){
		parent::populate('fee_coefficient_id');
	}
	
	public function setData($service_id, $person_id, $role, 
		$type, $fixed_value, $effective_date){
		$data	=	array(	
			'service_id'		=>	$service_id,
			'person_id'			=>	$person_id,
			'role'				=>	$role,
			'type'				=>	$type,
			'fixed_value'		=>	$fixed_value,
			'effective_date'	=>	$effective_date
		);
		$this->populateArray($data);
	}
	
	public function	getServiceId(){
		return $this->service_id; 
	}
	
	public function getRole(){
		return $this->role;
	}
	
	public function getType(){
		return $this->type;
	}
	
	public function getFeeCoefficientList($empId = 0){
	    $field      =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');				
		
		$ds		=	&new Datasource_sql();
		$sql	=	array(
			'cols'		=>	"c.fee_coefficient_id, c.service_id, g.$field as group_name, s.$field as service_name, 
			     c.role, c.type, c.fixed_value, c.effective_date", 			
			'from'		=>	"$this->_table c INNER JOIN service s ON c.service_id=s.service_id
						LEFT JOIN service_group g ON g.service_group_id=s.service_group_id",
			'where'		=>	"c.person_id=".(int)$empId,
			'orderby'	=>	"g.order, service_name"
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}	
	
	public function getInfoByServiceId($serviceId, $empId){
		$ds		=	&new Datasource_sql();
		$sql	=	array(
			'cols'		=>	"fee_coefficient_id, service_id, person_id, role, type, 
				fixed_value, effective_date",
			'from'		=>	"$this->_table",
			'where'		=>	"service_id=".(int)$serviceId." AND person_id=".(int)$empId
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function listEmployeeHasCoefficient(){
		$ds		=	&new Datasource_sql();
		$sql	=	array(
			'cols'		=>	"c.person_id as id, p.name as name",
			'from'		=>	"$this->_table c INNER JOIN employee e ON e.person_id=c.person_id 
				INNER JOIN person p ON p.person_id=e.person_id",
			'groupby'	=>	'c.person_id'
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray('id','name');
	}
	
	public function getEffectiveDate(){
        return L10nHelper::renderDateTime($this->effective_date);
	}
	
	public function delete($id,$delRelated = true){
		$sql	=	"DELETE FROM $this->_table WHERE $this->_key=".(int)$id;
		$this->_execute($sql);
		if($delRelated){
			$hourCoef	=	Celini::newORDO('HourCoefficient');
			$hourCoef->deleteRecordsByFeeCoefficientId($id);
		}
	}
	
	public function getIdsByInfo($serviceId, $personId = 0, $role=false){
		$ds		=	&new Datasource_sql();
		$sql	=	array(
			'cols'	=>	"fee_coefficient_id as id",
			'from'	=>	"$this->_table",
			'where'	=>	"service_id=$serviceId AND person_id=$personId"
		);
		if($role != false){
			$sql['where']	.=	" AND role='$role'"	;
		}
		
		$ds->setup($this->_db,$sql);
		return $ds->toArray('id');
	}
	
    public function getCoefficientsByInfo($coefDataList) {
        $field      =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');    	
        
        $where = '';
        $i = 0;
        foreach ($coefDataList as $item) {
            if ($i == 0) {
                $where .= "(fc.service_id='" . $item->getServiceId() . "' AND fc.person_id='" . $item->getPersonId()."'";
            } else {
                $where .= " OR (fc.service_id='" . $item->getServiceId() . "' AND fc.person_id='" . $item->getPersonId()."'";
            }
            if ($item->getRole() != false) {
                $where .= " AND fc.role='" . $item->getRole() . "'";
            }
            $where .= ")"; 
            $i++;
        }
        
        $ds       =   &new Datasource_sql();
        $sql    =   array(
            'cols'    => "fc.fee_coefficient_id, fc.service_id, fc.person_id, fc.role, fc.type, fc.fixed_value, fc.effective_date, "
                       . "s.$field as service_name",
            'from'    => "$this->_table fc"
                       . " LEFT JOIN service s ON fc.service_id = s.service_id",
            'where'   => $where,
            'groupby' => "fee_coefficient_id, service_id, person_id, role"          
        );        
        
        $ds->setup($this->_db,$sql);
        return $ds->toArray();
    }
}
?>